package com.woniuxy.mesboot.controller;

import com.woniuxy.mesboot.entity.SysMenu;
import com.woniuxy.mesboot.service.MenuService;
import com.woniuxy.mesboot.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ：lisir
 * @date ：Created in 2023/5/25 14:07
 * @description：TODO
 */
@RestController
@RequestMapping("/menus")
@Slf4j
public class MenuController {

    @Resource
    private MenuService menuService;

    @PostMapping()
    public Result add(@RequestBody SysMenu menu) {
        int i = menuService.insertSelective(menu);
        if (i > 0) {
            return Result.ok("添加成功");
        } else {
            return Result.error("添加失败");
        }
    }

    @DeleteMapping("{id}")
    public Result delete(@PathVariable Long id) {
        int i = menuService.deleteByPrimaryKey(id);
        if (i > 0) {
            return Result.ok("删除成功");
        } else {
            return Result.error("删除失败");
        }
    }

    @PutMapping()
    public Result update(@RequestBody SysMenu menu) {
        int i = menuService.updateByPrimaryKeySelective(menu);
        if (i > 0) {
            return Result.ok("修改成功");
        } else {
            return Result.error("用户名重复");
        }
    }

    @PostMapping("queryAll")
    public Result queryAll() {
        List<SysMenu> menus = menuService.selectAll();
        return Result.ok().data("list", menus);
    }
}
